package face.detector;

import android.graphics.Bitmap;
import face.entity.FaceFeaturePoint;
import face.entity.FaceFeatureRectangle;
import face.entity.FaceModel;

/**
 * Created by mazhi on 2018/7/14.
 */

public class FacePointsDetection {

    private native int loadModel(String modelDir);

    private native int[] detectPoints(byte[] rgb24, int width, int height, int widthStep, int[] face);
    private native int[] detectPointsForBitmap(Bitmap bmp, int width, int height, int[] face);

    private boolean _inited = false;

    private static volatile FacePointsDetection instance = null;

    private FacePointsDetection(){}

    public void init(String modelDir)
    {
        if (!_inited)
        {
            if (loadModel(modelDir)>0)
            {
                _inited=true;
            }
        }
    }

    public boolean getInited()
    {
        return _inited;
    }

    public static FacePointsDetection getInstance() {
        if (instance == null) {
            synchronized (FaceDetection.class) {
                if (instance == null) {
                    instance = new FacePointsDetection();
                }
            }
        }
        return instance;
    }

    public FaceFeaturePoint[] detectFacePoints(Bitmap bmp, int width, int height, FaceModel faceModel)
    {
        FaceFeaturePoint[] points=null;
        if (bmp!=null&&faceModel!=null)
        {
            int faceWidth=faceModel.rtFace.width;
            int faceHeight=faceModel.rtFace.height;
            int[] face={faceModel.rtFace.x,faceModel.rtFace.y,faceWidth,faceHeight};
            int[] pArray=detectPointsForBitmap(bmp,width,height,face);
            if (pArray!=null&&pArray[0]>0)
            {
                int length=pArray[0];
                points=new FaceFeaturePoint[length/2];
                int index=0;
                for (int i=0;i<length;)
                {

                    FaceFeaturePoint p=new FaceFeaturePoint();
                    p.x=pArray[1+i];
                    p.y=pArray[1+i+1];
                    points[index]=p;
                    index++;
                    i=i+2;
                }
            }
            int roll=pArray[1+pArray[0]];
            int yaw=pArray[1+pArray[0]+1];
            int pitch=pArray[1+pArray[0]+2];
            System.out.print(pitch);
        }
        return points;
    }
}
